package com.tudouji.project.farepay.controller;

import com.alibaba.fastjson.JSONObject;
import com.tudouji.common.constant.Constants;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.common.utils.http.HttpUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.farepay.domain.FarepayApply;
import com.tudouji.project.farepay.service.IFarepayApplyService;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.service.ISysConfigService;
import com.tudouji.project.system.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 物流运费付款申请Controller
 *
 * @author tudouji
 * @date 2021-10-27
 */
@RestController
@RequestMapping("/farepay/farepay")
public class FarepayApplyController extends BaseController
{
    @Autowired
    private IFarepayApplyService farepayApplyService;
    @Autowired
    private ITuserService tuserService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询物流运费付款申请列表
     */
//    @PreAuthorize("@ss.hasPermi('farepay:farepay:list')")
    @GetMapping("/list")
    public TableDataInfo list(FarepayApply farepayApply, String status, String functionType)
    {
        List<FarepayApply> list = farepayApplyService.selectFarepayApplyList(farepayApply, status, "16");
        BigDecimal amount = farepayApplyService.selectFarepayApplyListAmount(farepayApply, status, "16");
        return getDataTable(list, amount);
    }

    /**
     * 导出物流运费付款申请列表
     */
    @PreAuthorize("@ss.hasPermi('farepay:farepay:export')")
    @Log(title = "物流运费付款申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FarepayApply farepayApply, String stats, String functionType)
    {
        List<FarepayApply> list = farepayApplyService.selectFarepayApplyList(farepayApply, stats, functionType);
        ExcelUtil<FarepayApply> util = new ExcelUtil<FarepayApply>(FarepayApply.class);
        return util.exportExcel(list, "farepay");
    }

    /**
     * 获取物流运费付款申请详细信息
     * :获取付款申请详情数据接口
     */
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(farepayApplyService.selectFarepayApplyById(guid));
    }

    /**
     * 新增物流运费付款申请 :申请主表数据写入接口
     */
    @Log(title = "物流运费付款申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FarepayApply farepayApply)
    {
        return farepayApplyService.insertFarepayApply(farepayApply);
    }

    /**
     * 修改物流运费付款申请
     */
    @Log(title = "物流运费付款申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FarepayApply farepayApply)
    {
        return toAjax(farepayApplyService.updateFarepayApply(farepayApply));
    }

    /**
     * 删除物流运费付款申请
     */
//    @PreAuthorize("@ss.hasPermi('farepay:farepay:remove')")
    @Log(title = "物流运费付款申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(farepayApplyService.deleteFarepayApplyByIds(guids));
    }

    /**
     * 运费支付发起--调用老后台
     */
    @RepeatSubmit
    @PostMapping("/iSave")
    public AjaxResult iSave(@RequestBody FarepayApply apply) {
        try {
            FarepayApply applyPojo = farepayApplyService.selectFarepayApplyById(String.valueOf(apply.getId()));
            if (applyPojo.getTotalamount() == null || BigDecimal.ZERO.compareTo(applyPojo.getTotalamount()) == 0){
                return  AjaxResult.error("总金额不能为零！");
            }
            String phone = SecurityUtils.getLoginUser().getUser().getPhonenumber();
            Tuser tuser = tuserService.selectTuserByName(phone);
            apply.setUser(tuser);
            logger.info(configService.selectConfigByKey("potatoj_post_uri") + "/wlinfo/FarepayApply!iSave.action ----------params:" + JSONObject.toJSONString(apply));
            String result = HttpUtils.sendPost(configService.selectConfigByKey("potatoj_post_uri") + "/wlinfo/FarepayApply!iSave.action"
                    ,  "param=" +JSONObject.toJSONString(apply));
            JSONObject re = JSONObject.parseObject(result);
            String msg = re.getString("msg");
            logger.info(Constants.TUDOUJI + "/wlinfo/FarepayApply!iSave.action ----------msg:" + msg);
            if (StringUtils.equalsIgnoreCase("error",msg)){
                return  AjaxResult.error();
            }
            if (StringUtils.equalsIgnoreCase("outlogin",msg)){
                return  AjaxResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  AjaxResult.success();
    }

    /**
     * 发起
     **/
    @RepeatSubmit
    @PostMapping("/save")
    public AjaxResult save(Long id, String fileIds, @RequestParam(required = false)Long taskid
            , @RequestParam(required = false)String opinion) {
        return farepayApplyService.save(id, fileIds, taskid, opinion);
    }

    /**
     * 审批
     **/
    @RepeatSubmit
    @PostMapping("/approval")
    public AjaxResult  approval(Long id,Long taskid,String opinion,String fileIds){
        return farepayApplyService.approval(id,taskid,opinion,fileIds);
    }

    /**
     * 支付
     **/
    @RepeatSubmit
    @PostMapping("/pay")
    public AjaxResult  pay(Long id,Long taskid,String opinion,String fileIds){
        return farepayApplyService.pay(id,taskid,opinion,fileIds);
    }

    @RepeatSubmit
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid,Long id,String opinion,String fileIds,Long refuseNodeid,Long backUserid){
        return farepayApplyService.refuse(id, fileIds, opinion, taskid,refuseNodeid,backUserid);
    }

    /**
     * 微信--删除
     */
    @RepeatSubmit
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @PostMapping("del")
    public AjaxResult remove(Long id) {
        return farepayApplyService.del(id);
    }
}
